package com.yuzhitong.shapi.fragment;

import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.view.View;
import android.webkit.WebChromeClient;
import android.webkit.WebResourceRequest;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ProgressBar;

import com.yuzhitong.shapi.R;
import com.yuzhitong.shapi.activity.WebPageActivity;
import com.yuzhitong.shapi.base.BaseLayoutFragment;
import com.yuzhitong.shapi.base.Contents;
import com.yuzhitong.shapi.util.LoggerUtil;
import com.yuzhitong.shapi.util.StringUtil;
import com.yuzhitong.shapi.widget.MyWebView;

/**
 * Created by Brsya
 * CreateDate: 2022/5/10  20:53
 * Description:
 * 搜索结果Web页
 */
public class SearchWebFragment extends BaseLayoutFragment {

    private ProgressBar webProgress;
    private MyWebView webView;

    private int type;
    private String searchName;

    private boolean isPageLoadOk;
    private boolean isOpenWeb = false;

    private String loadUrl;

    public static SearchWebFragment newInstance(int type) {
        Bundle args = new Bundle();
        args.putInt("type", type);
        SearchWebFragment fragment = new SearchWebFragment();
        fragment.setArguments(args);
        return fragment;
    }
    public static SearchWebFragment newInstance(int type, String searchName) {
        Bundle args = new Bundle();
        args.putInt("type", type);
        args.putString("searchName", searchName);
        SearchWebFragment fragment = new SearchWebFragment();
        fragment.setArguments(args);
        return fragment;
    }

    public void setSearchName(String searchName) {
        if (searchName != null) {
            this.searchName = searchName;
            searchData();
        }
    }

    @Override
    protected void initView() {
        setContentViewId(R.layout.fragment_search_web);
        webProgress = findViewById(R.id.web_progress);
        webView = findViewById(R.id.web_view);

        webView.setWebChromeClient(new WebChromeClient() {

            @Override
            public void onProgressChanged(WebView view, int newProgress) {
                super.onProgressChanged(view, newProgress);
                if (newProgress < 100) {
                    webProgress.setVisibility(View.VISIBLE);
                    webProgress.setProgress(newProgress);
                } else {
                    webProgress.setVisibility(View.INVISIBLE);
                }
            }
        });

        webView.setWebViewClient(new WebViewClient() {
            //            @Nullable
            //            @Override
            //            public WebResourceResponse shouldInterceptRequest(WebView view, WebResourceRequest request) {
            //                Uri url = request.getUrl();
            ////                LoggerUtil.e("加载url:" + url);
            //                Intent intent = new Intent(getActivity(), WebPageActivity.class);
            //                intent.putExtra(Contents.INTENT_H5_URL, url);
            //                startActivity(intent);
            //                return null;
            //            }

            @Override
            public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
                String url = request.getUrl().toString();
                LoggerUtil.e("拦截url:" + url);
                if (url.indexOf("http") != 0 && url.indexOf("https") != 0 && url.indexOf("ftp") != 0) {
                    return true;
                }
                if (isPageLoadOk && !isOpenWeb) {
                    Intent intent = new Intent(getActivity(), WebPageActivity.class);
                    intent.putExtra(Contents.INTENT_H5_URL, url);
                    startActivity(intent);
                    isOpenWeb = true;
                }
                return isPageLoadOk;

                //                return super.shouldOverrideUrlLoading(view, request);
            }

            @Override
            public void onPageFinished(WebView view, String url) {
                super.onPageFinished(view, url);
                LoggerUtil.e("加载url成功:" + url);
                if (url.indexOf("http") != 0 && url.indexOf("https") != 0 && url.indexOf("ftp") != 0) {
                    return;
                }
                isPageLoadOk = true;
            }

            @Override
            public void onPageStarted(WebView view, String url, Bitmap favicon) {
                super.onPageStarted(view, url, favicon);
                LoggerUtil.e("开始加载url:" + url);
            }
        });
        webView.setCanScrollY(false);
    }

    @Override
    protected void initData() {
        if (getArguments() != null) {
            type = getArguments().getInt("type");
            switch (type) {
                case Contents.POSITION_MOVIE_BING:
                    loadUrl = Contents.SEARCH_URL_BING;
                    break;
                case Contents.POSITION_MOVIE_SHM:
                    loadUrl = Contents.SEARCH_URL_SHM;
                    break;
                case Contents.POSITION_MOVIE_BD:
                    loadUrl = Contents.SEARCH_URL_BD;
                    break;
                case Contents.POSITION_MOVIE_TT:
                    loadUrl = Contents.SEARCH_URL_TT;
                    break;
            }
            if (getArguments().getString("searchName") != null){
                this.searchName = getArguments().getString("searchName");
//                setSearchName(getArguments().getString("searchName"));
            }
        }
    }

    @Override
    public void onHiddenChanged(boolean hidden) {
        super.onHiddenChanged(hidden);
        if (!hidden) {
            if (StringUtil.isEmpty(webView.getUrl())) {
                searchData();
            }
        }
    }

    @Override
    public void onResume() {
        super.onResume();
        if (StringUtil.isEmpty(webView.getUrl())) {
            searchData();
        }
    }

    public void setCanScrollY(boolean canScrollY) {
        if (webView != null) {
            webView.setCanScrollY(canScrollY);
        }
    }

    @Override
    public void onStop() {
        super.onStop();
        isOpenWeb = false;
    }

    private void searchData() {
        if (StringUtil.isEmpty(searchName) || StringUtil.isEmpty(loadUrl)) {
            return;
        }

        webView.loadUrl(loadUrl + searchName);

    }
}
